<?php
/**
 * created by zzy
 * date: 2017/10/24 9:28
 */

namespace App\Api\Service\Qrcode\Controllers;

use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\Product;
use App\Eloquent\Ygt\Qrcode;
use App\Eloquent\Ygt\SellerQrcode;
use App\Eloquent\Ygt\Stock;
use Framework\BaseClass\Api\Controller;
use App\Engine\Func;

class IndexController extends Controller
{
    public $userId          = 25;
    //二维码通用扫描
    public function common()
    {
        $userId             = Func::getHeaderValueByName( 'userid',$this->userId );
        $userInfo           = DepartmentUser::getCurrentInfo( $userId );
        $qrcodeStr          = request('qrcode','');

        if(!$userInfo || !$qrcodeStr)
        {
            xThrow( ERR_PARAMETER );
        }
        //根据不同角色取不同表和模型
        $tableArr           = Func::getQrocdeTableArr($userInfo);
        $qrcodeModel        = $tableArr['qrcode_model'];
        $qrcodeFieldsModel  = $tableArr['fields_model'];
        $qrcodeLogModel     = $tableArr['log_model'];

        $qrcodeArr          = $qrcodeModel::decodeStr($qrcodeStr,false);
        if($qrcodeArr==false)
        {
            xThrow(ERR_UNKNOWN,'二维码类型出错');
        }
        $qrcodeId           = $qrcodeArr['id'];
        $qrcodeSn           = $qrcodeArr['sn'];
        $where              = ['id'=>$qrcodeId];
        $qrcode             = $qrcodeModel::getInfo($where);

        if(!$qrcode)
        {
            xThrow(ERR_UNKNOWN,'二维码信息出错');
        }
        $stockId            = $qrcode->stock_id;
        $productId          = $qrcode->product_id;
        //二维码的类型
        $qrcodeType         = $qrcodeModel::getQrcodeType($qrcode);
        //二维码的内容
        $qrcodeInfo         = [
            'id'=>$qrcode->id,
            'sn'=>$qrcode->sn,
            'product_id'=>$qrcode->product_id,
            'stock_id'=>$qrcode->stock_id,
        ];
        //按钮--不同权限显示不同,二维码类型不同显示也不同,默认全部显示
        $buttonListData     = [
            'ruku'=>['type'=>1,'title'=>'入库'],
            'chuku'=>['type'=>2,'title'=>'出库'],
            'bangding'=>['type'=>3,'title'=>'绑定'],
            'diaodu'=>['type'=>4,'title'=>'调度'],
        ];
        //权限过滤按钮
        //二维码类型过滤按钮
        switch ($qrcodeType)
        {
            case 1:
                break;
            case 2:
                unset($buttonListData['chuku']);
                break;
            case 3:
                unset($buttonListData['ruku']);
                unset($buttonListData['chuku']);
                break;
            default:
        }
        $buttonList         = array_values($buttonListData);
        $lists              = [];
        //二维码已经绑定过材料
        if($productId)
        {
            $where              = ['id'=>$productId];
            $product            = Product::getInfo($where);
            if(!$product)
            {
                xThrow(ERR_UNKNOWN,'材料信息出错');
            }
            $lists              = [
                ['left'=>'材料名称','right'=>$product->product_name],
                ['left'=>'材料编号','right'=>$product->product_no],
//                ['left'=>'堆位','right'=>$product->place_name],
            ];

            ////修改堆位信息
            //获取二维码对应的入库流水
            if($stockId){
                $placeName = '';
                $StockRow = \App\Eloquent\Ygt\Stock::where(['id'=>$stockId])->first();
                if($StockRow){
                    $placeName = $StockRow['place_name'];
                }

                $lists[] = ['left'=>'堆位','right'=>$placeName];
            }



            //添加数量信息
            if(isset($qrcode['now_number']) && ($qrcode['now_number'] || $qrcode['now_number'] ==='0')){
                $lists[] = ['left'=>'数量','right'=>$qrcode['now_number'].$product->unit];
            }

            ////添加基础信息
            //材料自定义属性
            $ProductFieldsModel = new \App\Eloquent\Ygt\ProductFields();
            $where = ['product_id' => $productId];
            $productFields = $ProductFieldsModel->getData($where);

            $productFields = $productFields->map(function ($item) {
                $data['field_name'] = $item->field_name;
                $comumnName = \App\Engine\Product::getFieldColumn($item->field_type);

                $data['field_value'] = $item->$comumnName;
                return $data;
            });

            if(!empty($productFields)){
                //顺序倒下
                $productFields = array_reverse($productFields->toArray());

                foreach ($productFields as $tmpRow){
                    $lists[] = ['left'=>$tmpRow['field_name'],'right'=>$tmpRow['field_value']];
                }
            }



        }

        $result             = [
            'qrcode_type'=>$qrcodeType,
            'qrcode_info'=>$qrcodeInfo,
            'product_id'=>$productId,
            'button_list'=>$buttonList,
            'lists'=>$lists,
        ];
        return $result;
    }
    //二维码类型是否空码
    public function checkEmpty()
    {
        $userId             = Func::getHeaderValueByName( 'userid',$this->userId );
        $userInfo           = DepartmentUser::getCurrentInfo( $userId );
        $qrcodeStr          = request('qrcode','');
        if(!$userInfo || !$qrcodeStr)
        {
            xThrow( ERR_PARAMETER );
        }
        //根据不同角色取不同表和模型
        $tableArr           = Func::getQrocdeTableArr($userInfo);
        $qrcodeModel        = $tableArr['qrcode_model'];
        $qrcodeFieldsModel  = $tableArr['fields_model'];
        $qrcodeLogModel     = $tableArr['log_model'];

        $qrcodeArr          = $qrcodeModel::decodeStr($qrcodeStr,false);
        if($qrcodeArr==false)
        {
            xThrow(ERR_UNKNOWN,'二维码解析出错');
        }
        $qrcodeId           = $qrcodeArr['id'];
        $where              = ['id'=>$qrcodeId];
        $qrcode             = $qrcodeModel::getInfo($where);
        if(!$qrcode)
        {
            xThrow(ERR_UNKNOWN,'二维码信息出错');
        }
        $qrcodeType         = $qrcodeModel::getQrcodeType($qrcode);
        if($qrcodeType!=3)
        {
            xThrow(ERR_UNKNOWN,'二维码已被使用');
        }
        $result             = [
            'message'=>'二维码可用',
            'qrcode_id'=>$qrcodeId,
        ];
        return $result;
    }
    //二维码扫描--入库
    public function in()
    {
        $userId             = Func::getHeaderValueByName( 'userid',$this->userId );
        $userInfo           = DepartmentUser::getCurrentInfo( $userId );
        $qrcodeStr          = request('qrcode','');
        if(!$userInfo || !$qrcodeStr)
        {
            xThrow( ERR_PARAMETER );
        }
        //根据不同角色取不同表和模型
        $tableArr           = Func::getQrocdeTableArr($userInfo);
        $qrcodeModel        = $tableArr['qrcode_model'];
        $qrcodeFieldsModel  = $tableArr['fields_model'];
        $qrcodeLogModel     = $tableArr['log_model'];

        $qrcodeArr          = $qrcodeModel::decodeStr($qrcodeStr,false);
        if($qrcodeArr==false)
        {
            xThrow(ERR_UNKNOWN,'二维码类型出错');
        }
        $qrcodeId           = $qrcodeArr['id'];
        $qrcodeSn           = $qrcodeArr['sn'];
        $where              = ['id'=>$qrcodeId];
        $qrcode             = $qrcodeModel::getInfo($where);
        if(!$qrcode)
        {
            xThrow(ERR_UNKNOWN,'二维码信息出错');
        }
        $stockId            = $qrcode->stock_id;
        if($stockId!=0)
        {
            xThrow(ERR_UNKNOWN,'二维码已被使用');
        }
        $result             = [
            'is_use'=>1,
            'message'=>'二维码可用'
        ];
        return $result;
    }
    //生成一个二维码
    public function create()
    {
        $userId             = Func::getHeaderValueByName( 'userid',$this->userId );
        $userInfo           = DepartmentUser::getCurrentInfo( $userId );
        $productId          = request('product_id', 0);
        if(!$userInfo || !$productId)
        {
            xThrow( ERR_PARAMETER );
        }
        $where          = ['id'=>$productId];
        $info           = Product::getInfo($where);
        if(!$info)
        {
            xThrow( ERR_UNKNOWN,'材料信息出错' );
        }
        $printResult    = \App\Engine\PrintEngine::productQrcodeByType($productId,[],1);
        $result['pinfo'] = $printResult;
        return $result;
    }
    public function add()
    {
        $productNo          = mt_rand(10000000,99999999);
        $qrcodeSn           = \App\Eloquent\Ygt\Qrcode::getSn($productNo);
        $qrcodeData         = ['sn' => $qrcodeSn, 'product_id' => 0, 'stock_id' => 0,];
        $qrcodeId           = \App\Eloquent\Ygt\Qrcode::insertOneData($qrcodeData, 'id');
        \App\Eloquent\Ygt\SellerQrcode::insertOneData($qrcodeData, 'id');
        $qrcodeStr          = '3-' . $qrcodeId . '-' . $qrcodeSn;
        echo $qrcodeStr;exit;
//        $qrcodePath         = 'upload/global/qrcode.png';
//        $qrcodePathHttp     = asset($qrcodePath);
//        \QrCode::format('png')->size(100)->generate($qrcodeStr,public_path($qrcodePath));
//        $style              = 'margin-top:200px;margin-left:50%;';
//        echo '<div style="'.$style.'"><img src="'.$qrcodePathHttp.'"></div>';exit;
    }
}